package com.sacc.service.Impl;

import com.power.common.model.CommonResult;
import com.sacc.mapper.BaseUserMapper;
import com.sacc.pojo.BaseUser;
import com.sacc.service.BaseUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author 滚~韬
 * @date 2022/4/9 15:28
 */
@Service
public class BaseUserServiceImpl implements BaseUserService {
    @Autowired
    private BaseUserMapper baseUserMapper;
    //添加用户基本信息
    public void Add(String openId){
        if(baseUserMapper.selectById(openId)==null){
            BaseUser baseUser = new BaseUser();
            baseUser.setCreateTime(LocalDateTime.now());
            baseUser.setOpenId(openId);
            baseUser.setRole(0);
            baseUserMapper.insert(baseUser);
        }
    }
    //添加电话号码
    public CommonResult AddPhoneNUmber(String openId,String phoneNumber){
        BaseUser base = baseUserMapper.selectById(openId);
        if(base==null){
            BaseUser baseUser = new BaseUser();
            baseUser.setOpenId(openId);
            baseUser.setPhoneNumber(phoneNumber);
            baseUser.setCreateTime(LocalDateTime.now());
            baseUser.setRole(0);
            baseUserMapper.insert(baseUser);
            return new CommonResult(true,"插入成功");
        }else {
            base.setPhoneNumber(phoneNumber);
            baseUserMapper.updateById(base);
            return new CommonResult(true,"更改电话号码成功",base);
        }
    }
    //得到电话号码
    public CommonResult GetPhoneNUmber(String openId){
        try{
            BaseUser baseUser = baseUserMapper.selectById(openId);
            if(baseUser.getPhoneNumber()==null){
                return new CommonResult(false,"400,该用户号码不存在");
            }else{
                return new CommonResult(true,"200",baseUser.getPhoneNumber());
            }
        }catch (Exception e){
            return new CommonResult(false,"获取用户号码失败");
        }

    }
}
